package com.wsx.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wsx.common.R;
import com.wsx.entity.Category;
import com.wsx.entity.Employee;
import com.wsx.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/category")
public class CategoryContorller {
    @Autowired
    private CategoryService categoryService;

    /**
     * 添加分类
     *      type=1代表菜品分类
     *      type=2代表套餐分类
     */
    @PostMapping
    public R<String> save(@RequestBody Category category){
        log.info("添加分类 {}",category);

        //categoryService.save(category);
        categoryService.insertCategory(category);
        return R.success("新增分类成功");
    }

    /**
     * 分页查询
     */
    @GetMapping("/page")
    public R<Page> mybatisPlusPage(int page , int pageSize ,String name){
        log.info("page = {},pageSize = {},name = {}" ,page,pageSize,name);
        //构造分页构造器
        Page pageInfo = new Page(page,pageSize);

        //构造条件构造器
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper();
        //添加过滤条件,前提是条件不为空，才会添加过滤条件
        queryWrapper.like(StringUtils.isNotEmpty(name),Category::getName,name);
        //添加排序条件
        queryWrapper.orderByAsc(Category::getSort);
        categoryService.page(pageInfo,queryWrapper);
        return R.success(pageInfo);
    }

    /**
     * 修改分类信息
     */
    @PutMapping
    public R<String> update(HttpServletRequest request , @RequestBody Category category){
        log.info(category.toString());
        long id = Thread.currentThread().getId();
        log.info("线程id为：{}",id);

        //categoryService.updateById(category);
        categoryService.updateByCategory(category);

        return R.success("修改分类信息成功");
    }

    /**
     * 根据id删除分类
     * @return
     */
    @DeleteMapping
    public R<String> delete(Long ids){
        log.info("删除分类，id为：{}",ids);

        //categoryService.removeById(ids);
        categoryService.remove(ids);

        return R.success("分类信息删除成功");
    }

    /**
     * 根据条件查询分类数据
     *      配合新增菜品，新增套餐进行操作，这里我们用Category来接收即可，因为type属性在Category中有
     */
    @GetMapping("/list")
    public R<List<Category>> list(Category category){
        log.info("按type条件为1或者2查,{}",category);

        //条件构造器
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        //添加条件
        queryWrapper.eq(category.getType() != null,Category::getType,category.getType());
        //添加排序条件
        queryWrapper.orderByAsc(Category::getSort).orderByDesc(Category::getUpdateTime);

        //底层就是调用Mapper的selectList查询
        List<Category> list = categoryService.list(queryWrapper);
        return R.success(list);
    }
}
